// pages/set/set.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneData:{
      activePhone:'',
      customerPhone:'',
      company:'',
      remarks:''
    },
    status:[
      {text:"一般客户",value:0},
      {text:"意向客户",value:1},
      {text:"无意向客户",value:2},
      {text:"成交客户",value:3},
      {text:"黑名单",value:4}
    ],
    currentStatus:'1'
  },

  //手机号输入框失焦时
  phoneUpdate(e){
    var $this = this
    $this.setData({
      ['phoneData.activePhone']:e.detail.value
    })
  },
  //姓名输入框失焦时
  customerPhoneUpdate(e){
    var $this = this
    $this.setData({
      ['phoneData.customerPhone']:e.detail.value
    })
  },
  //公司名字输入框失焦时
  companyUpdate(e){
      var $this = this
      $this.setData({
        ['phoneData.company']:e.detail.value
      })
    },
  //备注信息输入框失焦时
  remakesUpdate(e){
    var $this = this
    $this.setData({
      ['phoneData.remarks']:e.detail.value
    })
  },
  //状态选中
  complete(value){
    var $this =this
    $this.setData({
      currentStatus:value.detail
    })
  },

  //点击确认修改
  confirmUpdate(){
    wx.showModal({
      title: '提示',
      content: '是否确认修改！',
      success :(res) => {
        if (res.confirm) {
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/update', //仅为示例，并非真实的接口地址
      method:"POST",
      data: {
        activePhone:this.data.phoneData.activePhone,
        customerPhone:this.data.phoneData.customerPhone,
        company:this.data.phoneData.company,
        remarks:this.data.phoneData.remarks,
        status:this.data.currentStatus,
        userId:this.data.phoneData.userId,
        departId:this.data.phoneData.departId,
        roleId:this.data.phoneData.roleId,
        id:this.data.phoneData.id
      },
      header: {
        'content-type': 'application/json',// 默认值
        'X-Access-Token':token
      },
      success(res){
        wx.showToast({ 
          title: res.data.message, 
          icon: 'success', 
          duration: 1000 
          })
        wx.switchTab({
          url: '/pages/customer/customer',
        })
      },
      fail(){
        wx.showToast({ 
          title: '接口调用错误！', 
          icon: 'fail', 
          duration: 1000 
          })
      }
    })
    } else if (res.cancel) {
      console.log('用户点击取消')
    }
  }
  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let $this = this
    let id = JSON.parse(options.jsonStr)
    console.log(options.jsonStr)
    
    //findOne接口
    var token = wx.getStorageSync('TOKEN')
    wx.request({
      url: 'http://123.60.30.116:9999/app/phone/findOne', 
      method:"POST",
      data: {
       id:id
      },
      header: {
        'content-type': 'application/json', // 默认值
        'X-Access-Token':token
      },
      success(res){
        console.log(res.data)
        $this.setData({
          phoneData:res.data.data
        })
        $this.data.status.forEach(el => {
          if(el.value == res.data.data.status){
            $this.setData({
              currentStatus:+res.data.data.status
            })
            console.log($this.data.currentStatus)
          }
        });  
      },
      fail(){
        wx.showToast({ 
          title: '请返回上一页面重新进入！', 
          icon: 'loading', 
          duration: 1000 
          })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})